package com.appleframework.module.usermgr.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import com.appleframework.core.BaseContoller;
import com.appleframework.core.ServiceException;
import com.appleframework.module.usermgr.service.ItemDetailMgrService;
import com.appleframework.module.usermgr.service.ItemMgrService;
import com.appleframework.module.usermgr.vo.Item;
import com.appleframework.module.usermgr.vo.Itemdetail;
import com.appleframework.module.usermgr.vo.SUser;
import com.sun.org.apache.bcel.internal.generic.NEW;

/**
 * @author chaohua 菜品信息管理控制器
 */
@Controller
@RequestMapping(value = "itemdetailmgr")
public class ItemDetailMgr extends BaseContoller{

	private static final Logger log = Logger.getLogger(ItemDetailMgr.class);

	@Autowired
	private ItemDetailMgrService itemDetailMgrService;

	@Autowired
	private ItemMgrService itemMgrService;

	/**
	 * 分页所有菜品json数据
	 * 
	 * @return
	 */
	@RequestMapping("itemDetails.json.htm")
	@ResponseBody
	public Map getAllItemDetails(HttpServletRequest request) {
		
		//第一条记录的索引
		int start = new Integer(request.getParameter("start") == null ? "0"
				: request.getParameter("start"));
		//分页大小
		int limit = new Integer(request.getParameter("limit") == null ? "10"
				: request.getParameter("limit"));
		
		String itemDetailName = request.getParameter("itemDetailName");
		
		int parentid = new Integer(request.getParameter("parentid") == null
				|| request.getParameter("parentid").equals("") ? "-1" : request
				.getParameter("parentid"));
		
		Map<String, Object> result = new HashMap<String, Object>();

		result.put("itemdetails", itemDetailMgrService.getPage(start, limit,
				itemDetailName, parentid));

		result.put("totalCount", itemDetailMgrService.findRecordCount(
				itemDetailName, parentid));

		return result;
	}

	/***
	 * 返回菜系json
	 * 
	 * @return
	 */
	@RequestMapping("items.json.htm")
	@ResponseBody
	public List<Item> getAllItems() {
		try {
			return itemMgrService.getItems();
		} catch (ServiceException e) {
			e.printStackTrace();
		}
		return null;
	}

	/***
	 * 增加菜品
	 * 
	 * @param i
	 * @return
	 */
	@RequestMapping("itemdetail.save.htm")
	@ResponseBody
	public Map<String, Object> saveItemDetail(Itemdetail i) {
		itemDetailMgrService.addItemdetail(i);
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("success", true);
		return result;
	}

	/***
	 * 删除菜品
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("itemdetail.delete.htm")
	@ResponseBody
	public Map<String, Object> deleteItemDetail(HttpServletRequest request) {
		String[] temp = request.getParameterValues("menuId");
		for (String s : temp) {
			itemDetailMgrService.deleteItemDetail(Integer.valueOf(s));
		}
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("success", true);
		return result;
	}

	/***
	 * 修改
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("itemdetail.update.htm")
	@ResponseBody
	public Map<String, Object> getOneItemDetail(Itemdetail i) {

		itemDetailMgrService.updateItem(i);

		Map<String, Object> result = new HashMap<String, Object>();

		result.put("success", true);

		return result;
	}
	
	/***
	 * 增加菜品---图片
	 * 
	 * @param i
	 * @return
	 */
	@RequestMapping("itemdetail.img.htm")
	//@ResponseBody
	public String handleRequest(HttpServletRequest request,HttpServletResponse response) {
		String file_path1=new Fileupload().fileUpload(request, response);
		
		return "aaa";
	}
	
	//显示添加页面---
	@RequestMapping(value="add.htm")
	public String add(ModelMap model){
		try {
			List<Item> lItems1=itemMgrService.getItems();
			model.addAttribute("lItems1", lItems1);
		//System.out.println(lItems1.size());
			
			return "additemdetail";    
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "error";
			
		}

	}
	
}
